<template>
  <div class="app-container" style="padding: 5px 0 0 0">
    <el-form class="formList" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch"
             label-width="80px">
      <el-row>
        <!-- <el-col :span="8"> -->
          <el-form-item label="订单编号" prop="settlementNo">
            <el-input
              v-model="queryParams.settlementNo"
              placeholder="请输入结算编号"
              clearable
              size="small"
              style="width: 232px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        <!-- </el-col>
        <el-col :span="8"> -->
          <el-form-item label="下单时间" prop="auditTimeArray">
            <el-date-picker
              v-model="queryParams.auditTimeArray"
              type="daterange"
              align="right"
              value-format="yyyy-MM-dd"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width: 232px"
              :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
        <!-- </el-col>

        <el-col :span="8"> -->

          <el-form-item label="商品名称" prop="proName">
            <el-input
              v-model="queryParams.proName"
              placeholder="请输入结算编号"
              clearable
              size="small"
              style="width: 232px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

        <!-- </el-col> -->
      </el-row>

      <el-row>
        <el-form-item style="margin-left: 15px;">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
      </el-row>

    </el-form>
    <el-row class="row-mbinfo" style="margin-left: 0px; margin-right: 0px;">
      <el-row>

        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-bank-card"
            size="mini"
            @click="applySettlement"
            v-hasPermi="['supplier:settlement:list']"
          >申请结算
          </el-button>
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['supplier:info:export']"
            style="margin-left: 15px"
          >导出
          </el-button>
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
    </el-row>

    <div class="divTable">
      <el-table border v-loading="loading" :data="settlementList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="订单编号" align="center" prop="orderNo" min-width="150" :show-overflow-tooltip="true"/>
        <el-table-column label="商品名称" align="center" prop="proName" min-width="150" :show-overflow-tooltip="true"/>
        <el-table-column label="商品描述" align="center" prop="skuDesc" min-width="120" :show-overflow-tooltip="true"/>

        <el-table-column label="供应商价格（元）" width="150" align="center" prop="costPrice" :show-overflow-tooltip="true"/>
        <el-table-column label="销售数量" align="center" prop="itemNumber" :show-overflow-tooltip="true"/>
        <el-table-column label="售后数量" align="center" prop="serviceNumber" :show-overflow-tooltip="true"/>
        <el-table-column label="售后金额（元）" width="120" align="center" prop="serviceAmount" :show-overflow-tooltip="true"/>
        <el-table-column label="结算金额（元）" width="120" align="center" prop="settlementAmount" :show-overflow-tooltip="true"/>
        <el-table-column label="下单时间" align="center" prop="payTime" width="180" :show-overflow-tooltip="true"/>

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="140" fixed="right">
          <template slot-scope="scope">
<!--            <el-button-->
<!--              size="mini"-->
<!--              type="text"-->
<!--              icon="el-icon-zoom-in"-->
<!--              @click="handleShowDetail(scope.row)"-->
<!--              v-hasPermi="['supplier:settlement:query']"-->
<!--            >查看-->
<!--            </el-button>-->
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="applySettlement(scope.row)"
              v-hasPermi="['supplier:settlement:edit']"
            >申请结算
            </el-button>

          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 查看订单结算对话框 -->

    <orderDetails :viewOpen.sync="viewOpen"></orderDetails>
  </div>
</template>

<script>
  import {
    applySettlementList,
    applySettlement,
  } from '@/api/supplier/settlement'

  import {
    getSupplierList

  } from '@/api/supplier/info'
  import orderDetails from '@/views/supplier/settlement/orderDetails';

  export default {
    name: 'Settlement',
    components: {orderDetails},
    data() {
      return {
        //供应商列表
        supplierUidOptions: [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 订单结算表格数据
        settlementList: [],
        // 弹出层标题
        title: '',
        //查看详细标题
        viewTitle: '',
        // 是否显示弹出层
        open: false,
        //是否显示查看详细弹出层
        viewOpen: false,
        //查看详细数据
        showData: [],
        //对商品集合
        names: [],
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }]
        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          auditTimeArray: null,
          settlementNo: null,
          createTime: null,
          auditStatus: null,
          payStatus: null,
          payTime: null,
          receiveStatus: null,
          receiveTime: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      }
    },
    created() {
      this.getList()

      getSupplierList().then((response) => {
        this.supplierUidOptions = response.data
      })

    },
    methods: {
      /** 查询订单结算列表 */
      getList() {
        this.loading = true
        applySettlementList(this.queryParams).then(response => {
          this.settlementList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          settlementId: null,
          platformUid: null,
          supplierUid: null,
          settlementNo: null,
          settlementMoney: null,
          paymentMoney: null,
          orderNumber: null,
          productNumber: null,
          dataStatus: '0',
          createTime: null,
          createUser: null,
          updateTime: null,
          updateUser: null,
          auditStatus: '0',
          auditUser: null,
          auditTime: null,
          auditMsg: null,
          payStatus: null,
          payTime: null,
          payUser: null,
          receiveStatus: null,
          receiveTime: null,
          receiveUser: null
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        debugger
        this.ids = selection.map(item => item.itemUid)
        this.names = selection.map(item => item.proName)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 查看详情按钮操作 **/
      handleShowDetail(row) {
        this.viewOpen = true
        this.viewTitle = '查看订单结算'
        this.showData = []

      },
      handCloseDetail() {
        this.viewOpen = false
        this.showData = []
      },
      /** 申请结算操作 */
      applySettlement(row) {
        debugger
        const settlementIds = row.itemUid || this.ids
        const name = row.proName || this.names
        this.$confirm('您确认对商品名称为"' + name + '"进行结算吗?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return applySettlement(settlementIds)
        }).then(() => {
          this.getList()
          this.msgSuccess('提交成功')
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('supplier/settlement/export', {
          ...this.queryParams
        }, `supplier_settlement.xlsx`)
      }
    }
  }
</script>
